package com.javatiaocao.myblog.controller;

import com.javatiaocao.myblog.constant.CodeType;
import com.javatiaocao.myblog.model.FriendLink;
import com.javatiaocao.myblog.service.FriendLinkService;
import com.javatiaocao.myblog.utils.DataMap;
import com.javatiaocao.myblog.utils.JsonResult;
import com.javatiaocao.myblog.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class FriendLinkController {

    @Autowired
    FriendLinkService friendLinkService;

    /**
     *
     * 获取友联信息
     *
     * */
    @PostMapping("/getFriendLink")
    public String getFriendLink(){
        try {
            DataMap dataMap = friendLinkService.getFriendLink();
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getFriendLink is error"+e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }


    /**
     *
     * 更新或添加友联信息
     *
     * */
    @PostMapping("/updateFriendLink")
    public String updateFriendLink(FriendLink friendLink){
        try {
            //根据有无id判断是否新增
            if(!StringUtil.BLANK.equals(friendLink.getId())){
                DataMap dataMap = friendLinkService.updateFriendLink(friendLink);
                return JsonResult.build(dataMap).toJSON();
            }
            DataMap dataMap = friendLinkService.addFriendLink(friendLink);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("updateFriendLink is error" + e);
        }
        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     *删除友联
     *
     * */
    @PostMapping("/deleteFriendLink")
    public String deleteFriendLink(String id){
        try {
            DataMap dataMap = friendLinkService.deleteFriendLink(id);
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("deleteFriendLink is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }

    /**
     *
     * 友联链接详情
     *
     */
    @PostMapping("/getFriendLinkInfo")
    public String getFriendLinkInfo(){
        try {
            DataMap dataMap = friendLinkService.getFriendLinkInfo();
            return JsonResult.build(dataMap).toJSON();

        }catch (Exception e){
            log.error("getFriendLinkInfo is error"+e);
        }

        return JsonResult.fail(CodeType.SERVER_EXCEPTION).toJSON();
    }



}
